import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

function foo(name){
  // ;
  if(store.state.login.power){
    return store.state.login.power.some(item=>{
      // ;
      // console.log(item.power===name);
      return item.power===name
    })
  }else{
    return false
  }

}

const routes = [{
    path: '/login',
    name: 'login',
    component: () => import("../pages/Login/login.vue")
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import('../pages/Index/index.vue'),
    children: [{
      path: 'home',
      name: 'home',
      component: () => import('../views/home/home.vue'),
      beforeEnter(to,from,next){
        foo('/home')?next():next('/login')
      }
    },{
        path: 'role',
        name: 'role',
        component: () => import('../views/role/role.vue'),
        beforeEnter(to,from,next){
          foo('/role')?next():next('/index')
        }
      }, {
        path: 'power',
        name: 'power',
        component: () => import('../views/power/power.vue'),
        beforeEnter(to,from,next){
          foo('/power')?next():next('/index')
        }
      }, , {
        path: 'admin',
        name: 'admin',
        component: () => import('../views/admin/admin.vue'),
        beforeEnter(to,from,next){
          foo('/admin')?next():next('/index')
        }
      },
      {
        path: 'client',
        name: 'client',
        component: () => import('../views/client/client.vue'),
        beforeEnter(to,from,next){
          foo('/client')?next():next('/index')
        }
      }, {
        path: 'supply',
        name: 'supply',
        component: () => import('../views/supply/supply.vue'),
        beforeEnter(to,from,next){
          foo('/supply')?next():next('/index')
        }
      },{
        path: 'goods',
        name: 'goods',
        component: () => import('../views/goods/goods.vue'),
        beforeEnter(to,from,next){
          foo('/goods')?next():next('/index')
        }
      },{
        path: 'store',
        name: 'store',
        component: () => import('../views/store/store.vue'),
        beforeEnter(to,from,next){
          foo('/store')?next():next('/index')
        }
      },{
        path: 'put',
        name: 'put',
        component: () => import('../views/put/put.vue'),
        beforeEnter(to,from,next){
          foo('/put')?next():next('/index')
        }
      },{
        path: 'out',
        name: 'out',
        component: () => import('../views/out/out.vue'),
        beforeEnter(to,from,next){
          foo('/out')?next():next('/index')
        }
      },{
        path: 'stock',
        name: 'stock',
        component: () => import('../views/stock/stock.vue'),
        beforeEnter(to,from,next){
          foo('/stock')?next():next('/index')
        }
      },
      {
        path: '/index',
        redirect: '/index/home'
      }
    ]
  },
  {
    path:'*',
    redirect:'/login'
  }
]

const router = new VueRouter({
  routes
})

export default router